#include "UsrAI.h"
#include<set>
#include <iostream>
#include<unordered_map>
using namespace std;
tagGame tagUsrGame;
ins UsrIns;
//
//
tagInfo info;
int centrex=0,centrey=0;
int homex=0,homey=0;
int dockx=0,docky=0;
int farmx=0,farmy=0;
int stockx=0,stocky=0;
int landx=0,landy=0;
int newlandx=0,newlandy=0;
int goldx=0,goldy=0;
int armycampx=0,armycampy=0;
int docknum=0;
int farmernum=0;
int fishboatnum=0;
int armyboat=0;
int transportboat=0;
int homenum=0;
int stock=1;
int stable=0;
int armycamp=0;
int range=0;
int stoneman=0;
int horse=0;
int clubman=0;
int bowman=0;
int enemy=0;
int upgradearmy=0;
int woodskill=0;
int stoneskill=0;
int goldskill=0;
int usetool=0;
int infantrydefense=0;
int cavalrymandefense=0;
int archerydefense=0;
int state=0;
int wood;
int food;
int land=0;
int gold=0;
int newland=0;
int fishfarm=0;
int armynum=0;
char nowmap[128][128];
char newmap[128][128];
int farmerno[20];
int boatno[5];
int boatstate[4];
int farmerstate[20];
int ele[2];
map<int,tagFarmer> farmertable;
map<int,tagArmy> armyboatlist;
map<int,tagResource> elephant;
map<int,tagArmy> diearmy;
map<int,int> worknum;

int worknumcount(int sn)
{
    int s=0;
    for(tagFarmer&farmer:info.farmers)
        if(farmer.FarmerSort==0&&farmer.WorkObjectSN==sn)
            s++;
    return s;
}


void getmap()
{
    static int p=0;
    if(!p)
    {
        for(int i=0;i<128;i++)
        {
            for(int j=0;j<128;j++)
            {
                if((*info.theMap)[i][j].type==MAPPATTERN_UNKNOWN)
                    newmap[i][j]='?';
                else newmap[i][j]='*';
            }
        }
        p++;
    }
    for(int i=0;i<128;i++)
    {
        for(int j=0;j<128;j++)
        {
            if((*info.theMap)[i][j].type==MAPPATTERN_UNKNOWN)
                nowmap[i][j]='?';
            else if((*info.theMap)[i][j].type==MAPPATTERN_OCEAN)
                nowmap[i][j]='-';
            else if((*info.theMap)[i][j].type==MAPPATTERN_GRASS)
            {
                nowmap[i][j]=(*info.theMap)[i][j].height+'0';
                newland++;
            }
        }
    }
    for(tagResource& resource:info.resources)
    {
        if(resource.Type==RESOURCE_FISH||resource.Type==RESOURCE_GOLD||resource.Type==RESOURCE_STONE)
            for(int i=0;i<2;i++)
                for(int j=0;j<2;j++)
                    nowmap[resource.BlockDR+i][resource.BlockUR+j]='r';
        else
            nowmap[resource.BlockDR][resource.BlockUR]='r';
        worknum[resource.SN]=worknumcount(resource.SN);
        if(resource.Type==RESOURCE_GOLD)
            gold++;
    }
    for(tagFarmer&farmer:info.farmers)
        nowmap[farmer.BlockDR][farmer.BlockUR]='@';
    for(tagArmy&army:info.armies)
        nowmap[army.BlockDR][army.BlockUR]='@';
    for(tagBuilding&building:info.buildings)
    {
        if(building.Type==BUILDING_DOCK||building.Type==BUILDING_HOME||building.Type==BUILDING_ARROWTOWER)
            for(int i=0;i<2;i++)
                for(int j=0;j<2;j++)
                    nowmap[building.BlockDR+i][building.BlockUR+j]=building.Type+'a';
        else
            for(int i=0;i<3;i++)
                for(int j=0;j<3;j++)
                    nowmap[building.BlockDR+i][building.BlockUR+j]=building.Type+'a';;
    }
    for(int i=0;i<128;i++)
    {
        for(int j=0;j<128;j++)
        {
            if(newmap[i][j]!='*')
                newmap[i][j]=nowmap[i][j];
        }
    }
    wood=info.Wood;
    food=info.Meat;
    return;
}


int sn_to_obj(int n)
{
    for(tagResource&resource:info.resources)
        if(resource.SN==n)
            return resource.Type;
    for(tagFarmer&farmer:info.farmers)
        if(farmer.SN==n)
            return farmer.FarmerSort;
    for(tagArmy&army:info.armies)
        if(army.SN==n)
            return army.Sort;
    for(tagBuilding&building:info.buildings)
        if(building.SN==n)
            return building.Type;
}


void gethuman()
{
    for(tagFarmer&farmer:info.farmers)
    {
        if(farmer.FarmerSort==0)
        {
            if(farmertable.find(farmer.SN)==farmertable.end())
                farmerno[farmernum++]=farmer.SN;
            farmertable[farmer.SN]=farmer;
        }
        if(farmer.FarmerSort==1)
        {
            if(farmertable.find(farmer.SN)==farmertable.end())
                boatno[transportboat++]=farmer.SN;
            farmertable[farmer.SN]=farmer;
        }
    }
    for(tagArmy&army:info.armies)
    {
        if(army.Sort==AT_SHIP)
        {
            if(armyboatlist.find(army.SN)==armyboatlist.end())
                armyboat++;
            armyboatlist[army.SN]=army;
        }
        if(army.Sort!=AT_SHIP)
            armynum++;
    }
    for(tagArmy&enemyarmy:info.enemy_armies)
    {
        if(enemyarmy.Sort!=AT_SHIP&&enemyarmy.Blood<=5)
        {
            if(diearmy.find(enemyarmy.SN)==diearmy.end())
                enemy++;
            diearmy[enemyarmy.SN]=enemyarmy;
        }
    }
    return;
}


void getresource()
{
    for(tagResource&resource:info.resources)
    {
        if(resource.Type==RESOURCE_ELEPHANT)
        {
            if(!ele[0]||resource.SN==ele[0])
            {
                ele[0]=resource.SN;
                elephant[resource.SN]=resource;
                continue;
            }
            if(!ele[1]||resource.SN==ele[1])
            {
                ele[1]=resource.SN;
                elephant[resource.SN]=resource;
                continue;
            }
        }
    }
}


int surround(int x,int y)
{
    int s=0;
    if(nowmap[x][y+1]=='r')
        s++;
    if(nowmap[x][y-1]=='r')
        s++;
    if(nowmap[x+1][y]=='r')
        s++;
    if(nowmap[x-1][y]=='r')
        s++;
    return s;
}


int nearland(int x,int y)
{
    if(nowmap[x-1][y]=='0'||nowmap[x-1][y+1]=='0')
        return 1;
    if(nowmap[x+2][y]=='0'||nowmap[x+2][y+1]=='0')
        return 1;
    if(nowmap[x][y-1]=='0'||nowmap[x+1][y-1]=='0')
        return 1;
    if(nowmap[x+1][y+2]=='0'||nowmap[x][y+2]=='0')
        return 1;
    return 0;
}


int findresource(int r,int a,int b,int f=0)
{
    int x=0,y=0,m,n=0;
    for (tagResource&resource:info.resources)
    {
        if (resource.Type==r)
        {
            if(r==RESOURCE_TREE)
            {
                if(surround(resource.BlockDR,resource.BlockUR)>=4)
                    continue;
                if(worknum[resource.SN])
                    continue;
            }
            if(r==RESOURCE_GAZELLE)
            {
                if(surround(resource.BlockDR,resource.BlockUR)>=4)
                    continue;
                if(worknum[resource.SN]>=3)
                    continue;
            }
            if(r==RESOURCE_GOLD)
            {
                if(surround(resource.BlockDR,resource.BlockUR)>=4&&surround(resource.BlockDR+1,resource.BlockUR)>=4&&surround(resource.BlockDR,resource.BlockUR+1)>=4&&surround(resource.BlockDR+1,resource.BlockUR+1)>=4)
                    continue;
                if(worknum[resource.SN]>=5)
                    continue;
            }
            if(r==RESOURCE_ELEPHANT)
            {
                if(resource.Blood>0)
                    continue;
            }
            if(f==0&&r==RESOURCE_FISH&&!nearland(resource.BlockDR,resource.BlockUR))
                    continue;
            if(n==0)
            {
                n=resource.SN;
                x=resource.BlockDR-a;
                y=resource.BlockUR-b;
                m=x*x+y*y;
            }
            else
            {
                x=resource.BlockDR-a;
                y=resource.BlockUR-b;
                if(m>(x*x+y*y)&&(x*x+y*y)>0)
                {
                    m=x*x+y*y;
                    n=resource.SN;
                }
            }
        }
    }
    return n;
}


bool ifslope(char n,char a,int ty)
{
    if(n==a||n=='a'+ty) return 0;
    return 1;
}


bool ifspace(int x,int y,int ty)
{
    char a;
    if(ty==BUILDING_DOCK)
    {
        a=nowmap[x][y];
        if(a!='-') return 0;
        if(nowmap[x+1][y]!=a||nowmap[x][y+1]!=a||nowmap[x+1][y+1]!=a) return 0;
        if(nowmap[x+2][y]>='0'&&nowmap[x+2][y+1]>='0'&&nowmap[x+2][y]<='4'&&nowmap[x+2][y+1]<='4') return 1;
        else if(nowmap[x][y+2]>='0'&&nowmap[x+1][y+2]>='0'&&nowmap[x][y+2]<='4'&&nowmap[x+1][y+2]<='4') return 1;
        else if(nowmap[x-1][y]>='0'&&nowmap[x-1][y+1]>='0'&&nowmap[x-1][y]<='4'&&nowmap[x-1][y+1]<='4') return 1;
        else if(nowmap[x][y-1]>='0'&&nowmap[x+1][y-1]>='0'&&nowmap[x][y-1]<='4'&&nowmap[x+1][y-1]<='4') return 1;
        return 0;
    }
    else if(ty==BUILDING_ARROWTOWER||ty==BUILDING_HOME)
    {
        a=nowmap[x][y];
        if(a<'0'||a>'4') return 0;
        if(nowmap[x+1][y]!=a||nowmap[x][y+1]!=a||nowmap[x+1][y+1]!=a) return 0;
        if(ifslope(nowmap[x+2][y],a,ty)||ifslope(nowmap[x+2][y+1],a,ty)) return 0;
        else if(ifslope(nowmap[x][y+2],a,ty)||ifslope(nowmap[x+1][y+2],a,ty)) return 0;
        else if(ifslope(nowmap[x-1][y],a,ty)||ifslope(nowmap[x-1][y+1],a,ty)) return 0;
        else if(ifslope(nowmap[x][y-1],a,ty)||ifslope(nowmap[x+1][y-1],a,ty)) return 0;
        else if(ifslope(nowmap[x-1][y-1],a,ty)||ifslope(nowmap[x-1][y+2],a,ty)||ifslope(nowmap[x+2][y+2],a,ty)||ifslope(nowmap[x+2][y-1],a,ty)) return 0;
        return 1;
    }
    else
    {
        a=nowmap[x][y];
        if(a<'0'||a>'4') return 0;
        if(nowmap[x][y+1]!=a||nowmap[x][y+2]!=a||nowmap[x+1][y]!=a||nowmap[x+1][y+1]!=a||nowmap[x+1][y+2]!=a||nowmap[x+2][y]!=a||nowmap[x+2][y+1]!=a||nowmap[x+2][y+2]!=a)
            return 0;
        if(ifslope(nowmap[x+3][y],a,ty)||ifslope(nowmap[x+3][y+1],a,ty)||ifslope(nowmap[x+3][y+2],a,ty)) return 0;
        else if(ifslope(nowmap[x+2][y+3],a,ty)||ifslope(nowmap[x+1][y+3],a,ty)||ifslope(nowmap[x][y+3],a,ty)) return 0;
        else if(ifslope(nowmap[x-1][y],a,ty)||ifslope(nowmap[x-1][y+1],a,ty)||ifslope(nowmap[x-1][y+2],a,ty)) return 0;
        else if(ifslope(nowmap[x+2][y-1],a,ty)||ifslope(nowmap[x+1][y-1],a,ty)||ifslope(nowmap[x][y-1],a,ty)) return 0;
        else if(ifslope(nowmap[x-1][y-1],a,ty)||ifslope(nowmap[x-1][y+3],a,ty)||ifslope(nowmap[x+3][y+3],a,ty)||ifslope(nowmap[x+3][y-1],a,ty)) return 0;
        return 1;
    }
}


bool findarea(int&p,int&q)
{
    for(int i=0;i<128;i++)
    {
        for(int j=0;j<128;j++)
        {
            int x=i,y=j,ty=BUILDING_ARMYCAMP;
            char a=newmap[x][y];
            if(a<'0'||a>'4')
                continue;
            if(newmap[x][y+1]!=a||newmap[x][y+2]!=a||newmap[x+1][y]!=a||newmap[x+1][y+1]!=a||newmap[x+1][y+2]!=a||newmap[x+2][y]!=a||newmap[x+2][y+1]!=a||newmap[x+2][y+2]!=a)
                continue;
            if(ifslope(newmap[x+3][y],a,ty)||ifslope(newmap[x+3][y+1],a,ty)||ifslope(newmap[x+3][y+2],a,ty))
                continue;
            else if(ifslope(newmap[x+2][y+3],a,ty)||ifslope(newmap[x+1][y+3],a,ty)||ifslope(newmap[x][y+3],a,ty))
                continue;
            else if(ifslope(newmap[x-1][y],a,ty)||ifslope(newmap[x-1][y+1],a,ty)||ifslope(newmap[x-1][y+2],a,ty))
                continue;
            else if(ifslope(newmap[x+2][y-1],a,ty)||ifslope(newmap[x+1][y-1],a,ty)||ifslope(newmap[x][y-1],a,ty))
                continue;
            else if(ifslope(newmap[x-1][y-1],a,ty)||ifslope(newmap[x-1][y+3],a,ty)||ifslope(newmap[x+3][y+3],a,ty)||ifslope(newmap[x+3][y-1],a,ty))
                continue;
            p=x;
            q=y;
            return 1;
        }
    }
    return 0;
}


void findspace(int&x,int&y,int ty)
{
    int m=128*128+128*128;
    int a,b;
    a=x;
    b=y;
    for(int i=0;i<128;i++)
    {
        for(int j=0;j<128;j++)
        {
            if(ifspace(i,j,ty))
            {
                if(m>(i-a)*(i-a)+(j-b)*(j-b)&&(i-a)*(i-a)+(j-b)*(j-b)>=4)
                {
                    x=i;
                    y=j;
                    m=(i-a)*(i-a)+(j-b)*(j-b);
                }
            }
        }
    }
}


bool ifcomplete(int ty)
{
    for(tagBuilding&building:info.buildings)
        if(building.Type==ty&&building.Percent<100)
            return 0;
    for(tagBuilding&building:info.buildings)
        if(building.Type==ty)
            return 1;
    return 0;
}


bool ifcoast(int x,int y)
{
    if(nowmap[x][y]!='0') return 0;
    if(nowmap[x+1][y]=='-') return 1;
    if(nowmap[x-1][y]=='-') return 1;
    if(nowmap[x][y+1]=='-') return 1;
    if(nowmap[x][y-1]=='-') return 1;
    return 0;
}


void findcoast(int&x,int&y)
{
    int m=128*128+128*128;
    int a,b;
    a=x;
    b=y;
    for(int i=0;i<128;i++)
    {
        for(int j=0;j<128;j++)
        {
            if(ifcoast(i,j))
            {
                if(m>(i-a)*(i-a)+(j-b)*(j-b)&&(i-a)*(i-a)+(j-b)*(j-b)>5)
                {
                    x=i;
                    y=j;
                    m=(i-a)*(i-a)+(j-b)*(j-b);
                }
            }
        }
    }
    nowmap[x][y]='+';
    return;
}


bool findtree()
{
    for(tagResource&resource:info.resources)
        if(newmap[resource.BlockDR][resource.BlockUR]!='*'&&resource.Type==RESOURCE_TREE&&!worknum[resource.SN])
            return 1;
    return 0;
}


bool findfood()
{
    for(tagResource&resource:info.resources)
        if(newmap[resource.BlockDR][resource.BlockUR]!='*'&&resource.Type==RESOURCE_GAZELLE)
            return 1;
    return 0;
}


bool seaunknown()
{
    for(int j=1;j<127;j++)
        for(int i=1;i<127;i++)
            if(newmap[i][j]=='-'&&(newmap[i+1][j]=='?'||newmap[i][j+1]=='?'||newmap[i-1][j]=='?'||newmap[i][j-1]=='?'))
                return 1;
    return 0;
}


void UsrAI::findland()
{
    for(tagArmy&army:info.armies)
    {
        if(army.Sort==AT_SHIP&&army.NowState==HUMAN_STATE_IDLE)
        {
            if(newland>land+12)
            {
                newlandx=army.BlockDR;
                newlandy=army.BlockUR;
                build(farmertable[farmerno[0]],BUILDING_HOME,homex,homey);
                build(farmertable[farmerno[1]],BUILDING_HOME,homex,homey);
                build(farmertable[farmerno[2]],BUILDING_HOME,homex,homey);
                build(farmertable[farmerno[3]],BUILDING_HOME,homex,homey);
                homenum+=4;
                state++;
                break;
            }
            else if(centrex>=64&&centrey>=64)
                HumanMove(army.SN,0,0);
            else if(centrex<64&&centrey<64)
                HumanMove(army.SN,127*BLOCKSIDELENGTH,127*BLOCKSIDELENGTH);
            else if(centrex>=64&&centrey<64)
                HumanMove(army.SN,0,127*BLOCKSIDELENGTH);
            else if(centrex<64&&centrey>=64)
                HumanMove(army.SN,127*BLOCKSIDELENGTH,0);
        }
    }
    fighting();
}


void UsrAI::search()
{
    static int p=0;
    if(seaunknown())
    {
        for(tagArmy&army:info.armies)
        {
            if(army.Sort==AT_SHIP&&army.NowState==HUMAN_STATE_IDLE)
            {
                int x=-1,y=-1;
                int m=128*128+128*128;
                for(int j=1;j<127;j++)
                {
                    for(int i=1;i<127;i++)
                    {
                        if(newmap[i][j]=='-'&&(newmap[i+1][j]=='?'||newmap[i][j+1]=='?'||newmap[i-1][j]=='?'||newmap[i][j-1]=='?'))
                        {
                            if(m>(i-army.BlockDR)*(i-army.BlockDR)+(j-army.BlockUR)*(j-army.BlockUR))
                            {
                                x=i;
                                y=j;
                                m=(i-army.BlockDR)*(i-army.BlockDR)+(j-army.BlockUR)*(j-army.BlockUR);
                            }
                        }
                    }
                }
                if(x>=0&&y>=0)
                    HumanMove(army.SN,x*BLOCKSIDELENGTH+10.0,y*BLOCKSIDELENGTH+10.0);
            }
        }
    }
    else if(p++<8)
    {
        for(tagArmy&army:info.armies)
            if(army.Sort==AT_SHIP)
                HumanAction(army.SN,army.SN);
    }
    fighting();
}


void UsrAI::fishing()
{
    for(tagFarmer&farmer:info.farmers)
    {
        int n=findresource(RESOURCE_FISH,dockx,docky,1);
        int l1=0,l2=0;
        for(tagResource&resource:info.resources)
        {
            if(resource.SN==n)
                l2=(resource.BlockDR-dockx)*(resource.BlockDR-dockx)+(resource.BlockUR-docky)*(resource.BlockUR-docky);
            if(resource.SN==farmer.WorkObjectSN)
                l1=(resource.BlockDR-dockx)*(resource.BlockDR-dockx)+(resource.BlockUR-docky)*(resource.BlockUR-docky);
        }
        if(farmer.FarmerSort==2&&(l1>l2||farmer.NowState==HUMAN_STATE_IDLE))
            HumanAction(farmer.SN,n);
    }
    return;
}


void UsrAI::cuttree(tagFarmer&farmer)
{
    int sn=findresource(RESOURCE_TREE,farmer.BlockDR,farmer.BlockUR);
    HumanAction(farmer.SN,sn);
    worknum[sn]++;
    return;
}


void UsrAI::hunting(tagFarmer&farmer,int n)
{
    int x,y,sn;
    if(n==RESOURCE_FISH)
        x=dockx,y=docky;
    else x=farmer.BlockDR,y=farmer.BlockUR;
    sn=findresource(n,x,y);
    HumanAction(farmer.SN,sn);
    worknum[sn]++;
    return;
}


int step[2];
void UsrAI::killelephant(tagFarmer&farmer,int i)
{
    tagResource e=elephant[ele[i]];
    if(step[i]&&farmer.NowState==HUMAN_STATE_IDLE)
        HumanAction(farmer.SN,e.SN);
    if(!step[i])
    {
        if(nowmap[e.BlockDR-3][e.BlockUR-3]<='4'&&nowmap[e.BlockDR-3][e.BlockUR-3]>='0')
            HumanMove(farmer.SN,e.DR-2.9*BLOCKSIDELENGTH,e.UR-2.9*BLOCKSIDELENGTH);
        else if(nowmap[e.BlockDR+3][e.BlockUR-3]<='4'&&nowmap[e.BlockDR+3][e.BlockUR-3]>='0')
            HumanMove(farmer.SN,e.DR+2.9*BLOCKSIDELENGTH,e.UR-2.9*BLOCKSIDELENGTH);
        else if(nowmap[e.BlockDR-3][e.BlockUR+3]<='4'&&nowmap[e.BlockDR-3][e.BlockUR+3]>='0')
            HumanMove(farmer.SN,e.DR-2.9*BLOCKSIDELENGTH,e.UR+2.9*BLOCKSIDELENGTH);
        else if(nowmap[e.BlockDR+3][e.BlockUR+3]<='4'&&nowmap[e.BlockDR+3][e.BlockUR+3]>='0')
            HumanMove(farmer.SN,e.DR+2.9*BLOCKSIDELENGTH,e.UR+2.9*BLOCKSIDELENGTH);
        step[i]++;
    }
}


void UsrAI::build(tagFarmer&farmer,int ty,int&x,int&y)
{
    findspace(x,y,ty);
    HumanBuild(farmer.SN,ty,x,y);
    if(ty==BUILDING_ARROWTOWER||ty==BUILDING_HOME)
        for(int i=0;i<2;i++)
            for(int j=0;j<2;j++)
                nowmap[x+i][y+j]='B';
    else
        for(int i=0;i<3;i++)
            for(int j=0;j<3;j++)
                nowmap[x+i][y+j]='B';
    return;
}


void UsrAI::landing()
{
    for(int i=0;i<transportboat;i++)
    {

        if(((boatstate[i]==0&&farmertable[boatno[i]].NowState==HUMAN_STATE_IDLE)||boatstate[i]==1)&&farmertable[boatno[i]].Resource>0)
        {
            findcoast(newlandx,newlandy);
            if((*info.theMap)[newlandx-1][newlandy].type==MAPPATTERN_GRASS&&(*info.theMap)[newlandx][newlandy-1].type==MAPPATTERN_GRASS)
            {
                newlandx++;
                newlandy++;
                HumanMove(boatno[i],newlandx*BLOCKSIDELENGTH-1.0,newlandy*BLOCKSIDELENGTH-1.0);
                boatstate[i]=0;
            }
            else if((*info.theMap)[newlandx+1][newlandy].type==MAPPATTERN_GRASS&&(*info.theMap)[newlandx][newlandy+1].type==MAPPATTERN_GRASS)
            {
                HumanMove(boatno[i],newlandx*BLOCKSIDELENGTH+1.0,newlandy*BLOCKSIDELENGTH+1.0);
                boatstate[i]=0;
            }
            else if((*info.theMap)[newlandx+1][newlandy].type==MAPPATTERN_GRASS&&(*info.theMap)[newlandx][newlandy-1].type==MAPPATTERN_GRASS)
            {
                newlandy++;
                HumanMove(boatno[i],newlandx*BLOCKSIDELENGTH+1.0,newlandy*BLOCKSIDELENGTH-1.0);
                boatstate[i]=0;
            }
            else if((*info.theMap)[newlandx-1][newlandy].type==MAPPATTERN_GRASS&&(*info.theMap)[newlandx][newlandy+1].type==MAPPATTERN_GRASS)
            {
                newlandx++;
                HumanMove(boatno[i],newlandx*BLOCKSIDELENGTH-1.0,newlandy*BLOCKSIDELENGTH+1.0);
                boatstate[i]=0;
            }
        }
    }
}


void UsrAI::searcharea()
{
    int x,y;
    static int a=newlandx,b=newlandy;
    int m=128*128+128*128;
    for(int j=1;j<127;j++)
    {
        for(int i=1;i<127;i++)
        {
            if((newmap[i][j]>='0'&&newmap[i][j]<='4')&&(nowmap[i+1][j]=='?'||nowmap[i][j+1]=='?'||nowmap[i-1][j]=='?'||nowmap[i][j-1]=='?'))
            {
                if(m>(i-a)*(i-a)+(j-b)*(j-b)&&(i-a)*(i-a)+(j-b)*(j-b)>2)
                {
                    x=i;
                    y=j;
                    m=(i-a)*(i-a)+(j-b)*(j-b);
                }
            }
        }
    }
    for(int i=0;i<2;i++)
    {
        if(farmertable[farmerno[i]].NowState==HUMAN_STATE_IDLE)
        {
            HumanMove(farmerno[i],x*BLOCKSIDELENGTH,y*BLOCKSIDELENGTH);
            a=x;
            b=y;
        }
    }
    for(tagBuilding&building:info.buildings)
    {
        if((building.Type==BUILDING_ARMYCAMP||building.Type==BUILDING_RANGE||building.Type==BUILDING_STOCK||building.Type==BUILDING_STABLE)&&building.Percent<100)
        {
            for(int i=0;i<2;i++)
            {
                if(farmertable[farmerno[i]].WorkObjectSN!=building.SN)
                    HumanAction(farmerno[i],building.SN);
            }
            return;
        }
    }
    int p=0,q=0;
    if(armycamp==0||range==0||stock<3||stable==0)
    {
        if(findarea(p,q))
        {
            if(armycamp==0&&wood>=BUILD_ARMYCAMP_WOOD&&farmertable[farmerno[0]].NowState==HUMAN_STATE_WALKING)
            {
                HumanBuild(farmerno[0],BUILDING_ARMYCAMP,p,q);
                wood-=BUILD_ARMYCAMP_WOOD;
                armycamp++;
                armycampx=p;
                armycampy=q;
                return;
            }
            if(!range&&armycamp&&ifcomplete(BUILDING_ARMYCAMP)&&wood>=BUILD_RANGE_WOOD&&farmertable[farmerno[0]].NowState==HUMAN_STATE_WALKING)
            {
                HumanBuild(farmerno[0],BUILDING_RANGE,p,q);
                wood-=BUILD_RANGE_WOOD;
                range++;
                return;
            }
            if(armycamp&&!stable&&range&&wood>=BUILD_STABLE_WOOD&&farmertable[farmerno[0]].NowState==HUMAN_STATE_WALKING)
            {
                HumanBuild(farmerno[0],BUILDING_STABLE,p,q);
                wood-=BUILD_STABLE_WOOD;
                stable++;
                return;
            }
            if(stock<3&&armycamp&&range&&wood>BUILD_STOCK_WOOD&&farmertable[farmerno[0]].NowState==HUMAN_STATE_WALKING)
            {
                HumanBuild(farmerno[0],BUILDING_STOCK,p,q);
                stock++;
                wood-=BUILD_STOCK_WOOD;
                return;
            }
        }
    }
    else
    {
        if(gold)
        {
            for(int i=0;i<2;i++)
            {
                if(farmertable[farmerno[i]].NowState==HUMAN_STATE_IDLE&&sn_to_obj(farmertable[farmerno[i]].WorkObjectSN))
                {
                    mininggold(farmertable[farmerno[i]]);
                    return;
                }
            }
        }
        else if(gold==0&&findfood())
        {
            for(int i=0;i<2;i++)
            {
                if(farmertable[farmerno[i]].NowState==HUMAN_STATE_IDLE)
                {
                    hunting(farmertable[farmerno[i]],RESOURCE_GAZELLE);
                    return;
                }
            }
        }
        else if(gold==0&&!findfood()&&findtree())
        {
            for(int i=0;i<2;i++)
            {
                if(farmertable[farmerno[i]].NowState==HUMAN_STATE_IDLE)
                {
                    cuttree(farmertable[farmerno[i]]);
                    return;
                }
            }
        }
    }
}


void UsrAI::makearmy()
{
    if(!upgradearmy)
    {
        for(tagBuilding&building:info.buildings)
        {
            if(building.Type==BUILDING_ARMYCAMP&&building.Percent==100)
            {
                if(building.Project==0)
                {
                    if(food>=BUILDING_ARMYCAMP_UPGRADE_CLUBMAN_FOOD)
                    {
                        BuildingAction(building.SN,BUILDING_ARMYCAMP_UPGRADE_CLUBMAN);
                        upgradearmy=building.SN;
                        food-=BUILDING_ARMYCAMP_UPGRADE_CLUBMAN_FOOD;
                        break;
                    }
                }
            }
        }
    }
    for(tagBuilding&building:info.buildings)
    {
        if((usetool&&archerydefense&&infantrydefense&&cavalrymandefense)||enemy>25)
        {
            if(building.Type==BUILDING_ARMYCAMP&&building.Percent==100)
            {
                if(building.Project==0)
                {
                    if(food>=BUILDING_ARMYCAMP_CREATE_SLINGER_FOOD&&stoneman<5)
                    {
                        BuildingAction(building.SN,BUILDING_ARMYCAMP_CREATE_SLINGER);
                        food-=BUILDING_ARMYCAMP_CREATE_SLINGER_FOOD;
                        stoneman++;
                        continue;
                    }
                    else if(food>=BUILDING_ARMYCAMP_CREATE_CLUBMAN_FOOD&&clubman<10&&info.armies.size()<25)
                    {
                        BuildingAction(building.SN,BUILDING_ARMYCAMP_CREATE_CLUBMAN);
                        food-=BUILDING_ARMYCAMP_CREATE_CLUBMAN_FOOD;
                        clubman++;
                        continue;
                    }
                }
            }
            if(building.Type==BUILDING_RANGE&&building.Percent==100)
            {
                if(building.Project==0)
                {
                    if(food>=BUILDING_RANGE_CREATE_BOWMAN_FOOD&&wood>=BUILDING_RANGE_CREATE_BOWMAN_WOOD&&bowman<10&&info.armies.size()<25)
                    {
                        BuildingAction(building.SN,BUILDING_RANGE_CREATE_BOWMAN);
                        wood-=BUILDING_RANGE_CREATE_BOWMAN_WOOD;
                        food-=BUILDING_RANGE_CREATE_BOWMAN_FOOD;
                        bowman++;
                        continue;
                    }
                }
            }
            if(building.Type==BUILDING_STABLE&&building.Percent==100)
            {
                if(building.Project==0)
                {
                    if(food>=BUILDING_STABLE_CREATE_SCOUT_FOOD&&horse<2)
                    {
                        BuildingAction(building.SN,BUILDING_STABLE_CREATE_SCOUT);
                        food-=BUILDING_STABLE_CREATE_SCOUT_FOOD;
                        horse++;
                        continue;
                    }
                }
            }
        }
        if(building.Type==BUILDING_STOCK&&building.Project==0&&enemy<25)
        {
            if(food>=BUILDING_STOCK_UPGRADE_DEFENSE_INFANTRY_FOOD&&!infantrydefense)
            {
                BuildingAction(building.SN,BUILDING_STOCK_UPGRADE_DEFENSE_INFANTRY);
                food-=BUILDING_STOCK_UPGRADE_DEFENSE_INFANTRY_FOOD;
                infantrydefense++;
                continue;
            }
            if(food>=BUILDING_STOCK_UPGRADE_USETOOL_FOOD&&!usetool)
            {
                BuildingAction(building.SN,BUILDING_STOCK_UPGRADE_USETOOL);
                food-=BUILDING_STOCK_UPGRADE_USETOOL_FOOD;
                usetool++;
                continue;
            }
            if(food>=BUILDING_STOCK_UPGRADE_DEFENSE_ARCHER_FOOD&&!archerydefense)
            {
                BuildingAction(building.SN,BUILDING_STOCK_UPGRADE_DEFENSE_ARCHER);
                food-=BUILDING_STOCK_UPGRADE_DEFENSE_ARCHER_FOOD;
                archerydefense++;
                continue;
            }
            if(food>=BUILDING_STOCK_UPGRADE_DEFENSE_RIDER_FOOD&&!cavalrymandefense)
            {
                BuildingAction(building.SN,BUILDING_STOCK_UPGRADE_DEFENSE_ARCHER);
                food-=BUILDING_STOCK_UPGRADE_DEFENSE_RIDER_FOOD;
                cavalrymandefense++;
                continue;
            }
       }
       if(building.Type==BUILDING_MARKET&&building.Project==0)
       {
            if(food>=BUILDING_MARKET_STONE_UPGRADE_FOOD&&!stoneskill)
            {
                BuildingAction(building.SN,BUILDING_MARKET_STONE_UPGRADE);
                food-=BUILDING_MARKET_STONE_UPGRADE_FOOD;
                stoneskill++;
                continue;
            }
           if(food>=BUILDING_MARKET_GOLD_UPGRADE_FOOD&&!goldskill)
           {
               BuildingAction(building.SN,BUILDING_MARKET_GOLD_UPGRADE);
               food-=BUILDING_MARKET_GOLD_UPGRADE_FOOD;
               goldskill++;
               continue;
           }
       }
    }
}


void UsrAI::findgold()
{
    for(tagArmy&army:info.armies)
    {
        if(army.Sort!=AT_SHIP&&army.NowState==HUMAN_STATE_IDLE)
        {
            int x=-1,y=-1;
            int m=128*128+128*128;
            for(int j=1;j<127;j++)
            {
                for(int i=1;i<127;i++)
                {
                    if((newmap[i][j]>='0'&&newmap[i][j]<='4')&&(newmap[i+1][j]=='?'||newmap[i][j+1]=='?'||newmap[i-1][j]=='?'||newmap[i][j-1]=='?'))
                    {
                        if(m>(i-army.BlockDR)*(i-army.BlockDR)+(j-army.BlockUR)*(j-army.BlockUR))
                        {
                            x=i;
                            y=j;
                            m=(i-army.BlockDR)*(i-army.BlockDR)+(j-army.BlockUR)*(j-army.BlockUR);
                        }
                    }
                }
            }
            if(x>=0&&y>=0)
                HumanMove(army.SN,x*BLOCKSIDELENGTH+10.0,y*BLOCKSIDELENGTH+10.0);
        }
        if(gold>6)
        {
            state++;
            goldx=army.BlockDR;
            goldy=army.BlockUR;
            stockx=goldx;
            stocky=goldy;
            break;
        }
    }
    fighting();
}


void UsrAI::mininggold(tagFarmer&farmer)
{
    int sn=findresource(RESOURCE_GOLD,farmer.BlockDR,farmer.BlockUR);
    HumanAction(farmer.SN,sn);
    worknum[sn]++;
    return;
}


void UsrAI::getgold()
{
    for(tagBuilding&building:info.buildings)
    {
        if(building.Percent<100&&building.Type!=BUILDING_FARM)
        {
            for(int i=0;i<10;i++)
            {
                if(sn_to_obj(farmertable[farmerno[i]].WorkObjectSN)==RESOURCE_GOLD||farmertable[farmerno[i]].NowState==HUMAN_STATE_IDLE)
                    HumanAction(farmertable[farmerno[i]].SN,building.SN);
            }
            break;
        }
    }
    if(info.farmers.size()>=24&&boatstate[0]==0&&boatstate[1]==0&&boatstate[2]==0&&boatstate[3]==0)
    {
        if(stock>=5)
        {
            for(int i=0;i<farmernum;i++)
                if(farmertable[farmerno[i]].NowState==HUMAN_STATE_IDLE)
                    mininggold(farmertable[farmerno[i]]);
        }

        if(stock<5&&wood>=3*BUILD_STOCK_WOOD)
        {
            build(farmertable[farmerno[0]],BUILDING_STOCK,goldx,goldy);
            build(farmertable[farmerno[1]],BUILDING_STOCK,farmertable[farmerno[1]].BlockDR,farmertable[farmerno[1]].BlockUR);
            build(farmertable[farmerno[2]],BUILDING_STOCK,goldx,goldy);
            stock+=3;
        }

    }
    return;
}


void UsrAI::fighting()
{
    static int t=0;
    tagArmy*enemy_army=nullptr;
    for (tagArmy&army:info.enemy_armies)
    {
        if (army.Sort!=AT_SHIP&&army.NowState!=HUMAN_STATE_IDLE)
        {
            enemy_army=&army;
            break;
        }
    }
    if (enemy_army!=nullptr)
    {
        for (tagArmy&army:info.armies)
        {
            if (army.NowState!=HUMAN_STATE_ATTACKING)
            {
                HumanAction(army.SN,enemy_army->SN);
                return;
            }
        }
    }
    enemy_army=nullptr;
    for (tagArmy&army:info.enemy_armies)
    {
        if (army.Sort==AT_SHIP&&army.NowState!=HUMAN_STATE_IDLE)
        {
            enemy_army=&army;
            break;
        }
    }
    if (enemy_army!=nullptr)
    {
        for (tagArmy&army:info.armies)
        {
            if (army.Sort==AT_SHIP&&army.NowState!=HUMAN_STATE_ATTACKING)
            {
                HumanAction(army.SN,enemy_army->SN);
                return;
            }
        }
    }
}


void UsrAI::f0()
{
    for(int i=0;i<128;i++)
        for(int j=0;j<128;j++)
            if((*info.theMap)[i][j].type==MAPPATTERN_GRASS)
                land++;
    if (centrex==0&&centrey==0)
    {
        for (tagBuilding&building:info.buildings)
        {
            if (building.Type==BUILDING_CENTER)
            {
                centrex=building.BlockDR+1;
                centrey=building.BlockUR+1;
            }
            if(building.Type==BUILDING_STOCK)
            {
                stockx=building.BlockDR;
                stocky=building.BlockUR;
            }
        }
        homex=centrex;
        homey=centrey;
        dockx=centrex;
        docky=centrey;
        farmx=stockx;
        farmy=stocky;
        landx=centrex;
        landy=centrey;
    }

    build(farmertable[farmerno[0]],BUILDING_DOCK,dockx,docky);
    docknum++;
    build(farmertable[farmerno[1]],BUILDING_HOME,homex,homey);
    homenum++;
    killelephant(farmertable[farmerno[2]],0);
    farmerstate[2]=1;
    cuttree(farmertable[farmerno[3]]);
    state++;
    return;
}


void UsrAI::f1()
{
    static int upgrade=0;

    if(!upgrade)
    {
        for (tagBuilding&building:info.buildings)
        {
            if (building.Type==BUILDING_CENTER&&building.Project==0&&food>=500)
            {
                BuildingAction(building.SN,BUILDING_CENTER_UPGRADE);
                upgrade++;
                food-=500;
                int t=info.GameFrame/25;
                cout<<t/60<<':'<<(t%60)/10<<(t%60)%10<<endl;
                break;
            }
        }
        for(tagBuilding&building:info.buildings)
        {
            if(building.Percent<100&&building.Type==BUILDING_HOME&&farmertable[farmerno[1]].WorkObjectSN!=building.SN)
            {
                HumanAction(farmertable[farmerno[1]].SN,building.SN);
                break;
            }
        }
    }

    if(upgrade==2)
    {
        for(int i=0;i<farmernum;i++)
            if(farmertable[farmerno[i]].NowState==HUMAN_STATE_IDLE)
                cuttree(farmertable[farmerno[i]]);
        for(tagBuilding&building:info.buildings)
        {
            if(building.Percent<100)
            {
                for(int i=0;i<=3;i++)
                {
                    if(sn_to_obj(farmertable[farmerno[i]].WorkObjectSN)==RESOURCE_TREE)
                        HumanAction(farmertable[farmerno[i]].SN,building.SN);
                }
                break;
            }
        }
        if(info.civilizationStage==CIVILIZATION_TOOLAGE)
        {
            if(wood>BUILD_GRANARY_WOOD)
            {
                build(farmertable[farmerno[0]],BUILDING_GRANARY,stockx,stocky);
                state++;
            }
        }
        return;
    }

    if(upgrade==1)
    {
        for(tagFarmer&farmer:info.farmers)
            if(farmer.FarmerSort==0&&sn_to_obj(farmer.WorkObjectSN)!=RESOURCE_TREE)
                cuttree(farmer);
        if(wood>=3*BUILD_HOUSE_WOOD)
        {
            build(farmertable[farmerno[1]],BUILDING_HOME,homex,homey);
            build(farmertable[farmerno[2]],BUILDING_HOME,homex,homey);
            build(farmertable[farmerno[3]],BUILDING_HOME,homex,homey);
            homenum+=3;
            upgrade++;
        }
        return;
    }
    if(farmertable[farmerno[0]].NowState==HUMAN_STATE_IDLE)
       hunting(farmertable[farmerno[0]],RESOURCE_ELEPHANT);
    if(farmertable[farmerno[1]].NowState==HUMAN_STATE_IDLE&&homenum>=3&&stock>=2)
        hunting(farmertable[farmerno[1]],RESOURCE_ELEPHANT);
    else if(farmertable[farmerno[1]].NowState==HUMAN_STATE_IDLE&&homenum>=3&&stock<2)
        cuttree(farmertable[farmerno[1]]);
    if(homenum<3)
    {
        if(farmertable[farmerno[1]].NowState==HUMAN_STATE_IDLE&&wood>=BUILD_HOUSE_WOOD&&info.Human_MaxNum==homenum*4+4)
        {
            build(farmertable[farmerno[1]],BUILDING_HOME,homex,homey);
            homenum++;
            wood-=BUILD_HOUSE_WOOD;
        }
    }
    if(stock<2&&homenum>=3)
    {
        if(sn_to_obj(farmertable[farmerno[1]].WorkObjectSN)!=BUILDING_HOME&&wood>=BUILD_STOCK_WOOD)
        {
            for(tagResource&resource:info.resources)
            {
                if(resource.Type==RESOURCE_ELEPHANT)
                {
                    build(farmertable[farmerno[1]],BUILDING_STOCK,resource.BlockDR,resource.BlockUR);
                    wood-=BUILD_STOCK_WOOD;
                    stock++;
                    break;
                }
            }
        }
    }
    if(elephant[ele[0]].Blood==0)
    {
        if(elephant[ele[1]].Blood>0)
            killelephant(farmertable[farmerno[2]],1);
        else
        {
            farmerstate[2]=0;
            if(farmertable[farmerno[2]].NowState==HUMAN_STATE_IDLE)
                hunting(farmertable[farmerno[2]],RESOURCE_ELEPHANT);
        }
    }
    else
        killelephant(farmertable[farmerno[2]],0);
    if(farmernum<12)
    {
        for (tagBuilding&building:info.buildings)
        {
            if (building.Type==BUILDING_CENTER&&building.Project==0&&food>=BUILDING_CENTER_CREATEFARMER_FOOD&&info.farmers.size()<info.Human_MaxNum)
            {
                BuildingAction(building.SN,BUILDING_CENTER_CREATEFARMER);
                food-=BUILDING_CENTER_CREATEFARMER_FOOD;
                break;
            }
        }
    }

    if(fishboatnum<3&&homenum>=3)
    {
        for(tagBuilding&building:info.buildings)
        {
            if(building.Type==BUILDING_DOCK&&building.Percent==100)
            {
               if(building.Project==0&&wood>=BUILDING_DOCK_CREATE_SAILING_WOOD&&info.farmers.size()<info.Human_MaxNum)
               {
                   BuildingAction(building.SN,BUILDING_DOCK_CREATE_SAILING);
                   fishboatnum++;
                   wood-=BUILDING_DOCK_CREATE_SAILING_WOOD;
                   break;
               }
            }
        }
    }

    for (int i=3;i<farmernum;i++)
    {
       if(i%2&&farmertable[farmerno[i]].NowState==HUMAN_STATE_IDLE)
           hunting(farmertable[farmerno[i]],RESOURCE_ELEPHANT);
       else if(farmertable[farmerno[i]].NowState==HUMAN_STATE_IDLE)
           cuttree(farmertable[farmerno[i]]);
    }
    fishing();
    return;
}


void UsrAI::f2()
{
    static int market=0;
    for(int i=0;i<farmernum;i++)
    {
        if(farmertable[farmerno[i]].NowState==HUMAN_STATE_IDLE)
            cuttree(farmertable[farmerno[i]]);
    }
    if(food<100&&!woodskill&&market&&docknum==2)
    {
        if(farmertable[farmerno[0]].NowState==HUMAN_STATE_IDLE||sn_to_obj(farmertable[farmerno[0]].WorkObjectSN)==RESOURCE_TREE)
            hunting(farmertable[farmerno[0]],RESOURCE_GAZELLE);
    }
    else if(woodskill)
        if(sn_to_obj(farmertable[farmerno[0]].WorkObjectSN)!=RESOURCE_TREE&&sn_to_obj(farmertable[farmerno[0]].WorkObjectSN)!=BUILDING_DOCK)
            cuttree(farmertable[farmerno[0]]);
    for(tagBuilding&building:info.buildings)
    {
        if(building.Percent<100)
        {
            for(int i=0;i<=3;i++)
            {
                if(sn_to_obj(farmertable[farmerno[i]].WorkObjectSN)==RESOURCE_TREE)
                    HumanAction(farmertable[farmerno[i]].SN,building.SN);
            }
            break;
        }
    }
    if(!market)
    {
        for(tagBuilding&building:info.buildings)
        {
            if(building.Type==BUILDING_GRANARY)
            {
                if(building.Percent==100&&wood>=BUILD_MARKET_WOOD)
                {
                    build(farmertable[farmerno[0]],BUILDING_MARKET,stockx,stocky);
                    wood-=BUILD_MARKET_WOOD;
                    market=1;
                }
            }
        }
    }
    for(tagBuilding&building:info.buildings)
    {
        if(building.Type==BUILDING_MARKET&&building.Percent==100)
        {
            market=2;
            if(building.Project==0)
            {
                if(food>=120&&wood>=75&&woodskill==0)
                {
                    BuildingAction(building.SN,BUILDING_MARKET_WOOD_UPGRADE);
                    woodskill++;
                    food-=120;
                    wood-=75;
                }
            }
        }
    }
    if(market&&docknum<2)
    {
        if(wood>=BUILD_DOCK_WOOD&&sn_to_obj(farmertable[farmerno[1]].NowState)!=BUILDING_DOCK)
        {
            build(farmertable[farmerno[1]],BUILDING_DOCK,dockx,docky);
            docknum++;
            wood-=BUILD_DOCK_WOOD;
        }
    }
    if(armyboat<10&&armyboat+info.farmers.size()<info.Human_MaxNum&&market)
    {
        for(tagBuilding&building:info.buildings)
        {
            if(building.Type==BUILDING_DOCK&&building.Percent==100)
            {
               if(building.Project==0&&wood>=BUILDING_DOCK_CREATE_SHIP_WOOD&&info.farmers.size()+info.armies.size()<info.Human_MaxNum)
               {
                   BuildingAction(building.SN,BUILDING_DOCK_CREATE_SHIP);
                   wood-=BUILDING_DOCK_CREATE_SHIP_WOOD;
                   continue;
               }
            }
        }
    }
    else if(armyboat+info.farmers.size()>=28||armyboat>=10) findland();

    if(farmernum<16&&woodskill)
    {
        for (tagBuilding&building:info.buildings)
        {
            if (building.Type==BUILDING_CENTER&&building.Project==0&&food>=BUILDING_CENTER_CREATEFARMER_FOOD&&info.farmers.size()<info.Human_MaxNum)
            {
                BuildingAction(building.SN,BUILDING_CENTER_CREATEFARMER);
                food-=BUILDING_CENTER_CREATEFARMER_FOOD;
                break;
            }
        }
    }
    fishing();
    return;
}


void UsrAI::f3()
{   
    static int farm=0;
    static int outsea=0;
    static int out=0;
    search();
    fishing();
    for(int i=2;i<farmernum;i++)
        if(farmertable[farmerno[i]].NowState==HUMAN_STATE_IDLE)
            cuttree(farmertable[farmerno[i]]);
    for(tagBuilding&building:info.buildings)
    {
        if(building.Percent<100&&building.Type==BUILDING_HOME)
        {
            for(int i=2;i<=3;i++)
            {
                if(sn_to_obj(farmertable[farmerno[i]].WorkObjectSN)==RESOURCE_TREE)
                    HumanAction(farmertable[farmerno[i]].SN,building.SN);
            }
            break;
        }
    }
    if(transportboat<1)
    {
        for(tagBuilding&building:info.buildings)
        {
            if(building.Type==BUILDING_DOCK&&building.Project==0)
            {
                if(wood>=BUILDING_DOCK_CREATE_WOOD_BOAT_WOOD)
                {
                    BuildingAction(building.SN,BUILDING_DOCK_CREATE_WOOD_BOAT);
                    wood-=BUILDING_DOCK_CREATE_WOOD_BOAT_WOOD;
                    break;
                }
            }
        }
    }
    if(!outsea&&transportboat>=1)
    {
        if(farmertable[boatno[0]].Resource==0&&farmertable[boatno[0]].NowState==HUMAN_STATE_IDLE&&!boatstate[0])
        {
            findcoast(landx,landy);
            HumanMove(boatno[0],BLOCKSIDELENGTH*landx+10.0,BLOCKSIDELENGTH*landy+10.0);
            HumanAction(farmerno[0],boatno[0]);
            HumanAction(farmerno[1],boatno[0]);
            boatstate[0]=1;
        }
        if(farmertable[boatno[0]].Resource==2&&boatstate[0]==1)
        {
            outsea++;
            landx=centrex;
            landy=centrey;
        }
    }
    if(outsea)
    {
        landing();
        if(farmertable[boatno[0]].Resource==0&&farmertable[boatno[0]].NowState==HUMAN_STATE_IDLE&&!boatstate[0])
        {
            findcoast(landx,landy);
            HumanMove(boatno[0],BLOCKSIDELENGTH*landx+15.0,BLOCKSIDELENGTH*landy+15.0);
            boatstate[0]=1;
        }
        searcharea();
    }
    if(wood<150&&(!armycamp||!range||!stable||stock<3))
    {
        for(int i=2;i<farmernum;i++)
        {
            if(farmertable[farmerno[i]].NowState==HUMAN_STATE_IDLE&&sn_to_obj(farmertable[farmerno[i]].WorkObjectSN)!=BUILDING_FARM)
                cuttree(farmertable[farmerno[i]]);
        }
    }
    if(homenum<11&&wood>=BUILD_HOUSE_WOOD)
    {
        build(farmertable[farmerno[2]],BUILDING_HOME,homex,homey);
        wood-=BUILD_HOUSE_WOOD;
        homenum++;
    }
    for(int i=4;i<8;i++)
    {
        if((elephant[ele[0]].Cnt>0||elephant[ele[1]].Cnt>0)&&farmertable[farmerno[i]].NowState==HUMAN_STATE_IDLE)
        {
            hunting(farmertable[farmerno[i]],RESOURCE_ELEPHANT);
            continue;
        }
        if(farm<6&&wood>=BUILD_FARM_WOOD&&sn_to_obj(farmertable[farmerno[i]].WorkObjectSN)!=BUILDING_FARM&&(armycamp&&range&&stock>=3&&stable)||wood>=550)
        {
            build(farmertable[farmerno[i]],BUILDING_FARM,farmx,farmy);
            farm++;
            wood-=BUILD_FARM_WOOD;
            continue;
        }
        else if(farmertable[farmerno[i]].NowState==HUMAN_STATE_IDLE)
            cuttree(farmertable[farmerno[i]]);
    }

    for(int i=8;i<farmernum;i++)
    {
        if(food<500&&armycamp&&range&&stock>=3&&stable)
        {
            if(sn_to_obj(farmertable[farmerno[i]].WorkObjectSN)!=RESOURCE_GAZELLE)
                hunting(farmertable[farmerno[i]],RESOURCE_GAZELLE);
        }
        else if(sn_to_obj(farmertable[farmerno[i]].WorkObjectSN)!=RESOURCE_TREE)
            cuttree(farmertable[farmerno[i]]);
    }
    if(armycamp)
        makearmy();
    if((armynum>=25&&usetool&&archerydefense&&infantrydefense&&cavalrymandefense)||(armynum+enemy>=30&&armynum>=5)||out)
    {
        out=1;
        findgold();
        if(farmernum<17)
        {
            for(tagBuilding&building:info.buildings)
            {
                if(building.Type==BUILDING_CENTER&&building.Project==0)
                {
                    if(food>=BUILDING_CENTER_CREATEFARMER_FOOD&&info.farmers.size()+info.armies.size()<info.Human_MaxNum)
                    {
                        BuildingAction(building.SN,BUILDING_CENTER_CREATEFARMER);
                        food-=BUILDING_CENTER_CREATEFARMER_FOOD;
                    }
                }
            }
        }
    }
    if(armycamp&&range&&stock>=3&&farm>=4)
    {
        if(transportboat<3)
        {
            for(tagBuilding&building:info.buildings)
            {
                if(building.Type==BUILDING_DOCK&&building.Project==0)
                {
                    if(wood>=BUILDING_DOCK_CREATE_WOOD_BOAT_WOOD)
                    {
                        BuildingAction(building.SN,BUILDING_DOCK_CREATE_WOOD_BOAT);
                        wood-=BUILDING_DOCK_CREATE_WOOD_BOAT_WOOD;
                    }
                }
            }
        }
    }
    if(info.farmers.size()+info.armies.size()==48&&homenum<12&&wood>=BUILD_HOUSE_WOOD)
    {
        build(farmertable[farmerno[0]],BUILDING_HOME,homex,homey);
        wood-=BUILD_HOUSE_WOOD;
        homenum++;
    }
    return;
}


void UsrAI::f4()
{
    static int p=0;
    for(tagArmy&army:info.armies)
    {
        if(army.Sort!=AT_SHIP&&army.NowState==HUMAN_STATE_IDLE)
        {
            int x,y;
            int m=128*128+128*128;
            for(int j=1;j<127;j++)
            {
                for(int i=1;i<127;i++)
                {
                    if((*info.theMap)[i][j].type==MAPPATTERN_GRASS&&(nowmap[i+1][j]=='?'||nowmap[i][j+1]=='?'||nowmap[i-1][j]=='?'||nowmap[i][j-1]=='?'))
                    {
                        if(m>(i-army.BlockDR)*(i-army.BlockDR)+(j-army.BlockUR)*(j-army.BlockUR)&&(i-army.BlockDR)*(i-army.BlockDR)+(j-army.BlockUR)*(j-army.BlockUR)>5)
                        {
                            x=i;
                            y=j;
                        }
                    }
                }
            }
            HumanMove(army.SN,x*BLOCKSIDELENGTH+10.0,y*BLOCKSIDELENGTH+10.0);
        }
    }
    fighting();
    if(farmernum==20&&!p)
    {
        for(int i=0;i<4;i++)
            if(farmertable[boatno[i]].Resource<5&&boatstate[i]==1)
                for(int j=i*5;j<i*5+5;j++)
                    HumanAction(farmerno[j],boatno[i]);
        p++;
    }
    if(farmernum<20)
    {
        for(tagBuilding&building:info.buildings)
        {
            if(building.Type==BUILDING_CENTER&&building.Project==0&&food>=BUILDING_CENTER_CREATEFARMER_FOOD&&info.armies.size()+info.farmers.size()<=info.Human_MaxNum)
            {
                BuildingAction(building.SN,BUILDING_CENTER_CREATEFARMER);
                food-=BUILDING_CENTER_CREATEFARMER_FOOD;
            }
        }
    }
    if(p==1&&farmertable[boatno[0]].Resource==5&&farmertable[boatno[1]].Resource==5&&farmertable[boatno[2]].Resource==5&&farmertable[boatno[3]].Resource==5)
        p++;
    if(p==2)
    {
        landing();
        getgold();
    }
    return;
}


void UsrAI::processData()
{
    info=getInfo();
    newland=0;
    gold=0;
    armynum=0;
    getmap();
    gethuman();
    getresource();

    switch (state)
    {
    case 0:f0();
           break;
    case 1:f1();
           break;
    case 2:f2();
           break;
    case 3:f3();
           break;
    case 4:f4();
           break;
    }

}


